package com.yangye.niodemo.rpc;

import com.yangye.niodemo.rpc.client.RpcClient;
import com.yangye.niodemo.rpc.client.TestRpcClient;
import com.yangye.niodemo.rpc.proxy.RpcProxy;
import com.yangye.niodemo.rpc.server.RpcServer;
import com.yangye.niodemo.rpc.transport.ServerDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yangye
 * @date 2020/12/29
 */
public class RpcTest {

	@Test
	public void clientTest() {
		AtomicInteger num = new AtomicInteger(0);
		int size = 10000;
		Thread[] threads = new Thread[size];
		for (int i = 0; i < size; i++) {
			threads[i] = new Thread(() -> {
				TestRpcClient car = RpcProxy.proxyGet(TestRpcClient.class);//动态代理实现
				String arg = "hello" + num.incrementAndGet();
				String res = car.test(arg);
				System.out.println("client over msg: " + res + " src arg: " + arg);
			});
		}

		for (Thread thread : threads) {
			thread.start();
		}

		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void startServer() {
		RpcServer rpcServer = new RpcServer(9090);
		rpcServer.start();
	}
}
